package com.jvyou.app.ds.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.domain.model.Result;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.common.utils.value.ValueChecker;
import com.jvyou.app.ds.domain.dto.StudentDto;
import com.jvyou.app.ds.domain.entity.Student;
import com.jvyou.app.ds.service.StudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学员信息表(Student)表控制层
 *
 * @author 橘柚
 * @since 2025-04-17 15:30:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/student")
public class StudentController {

    private final StudentService studentService;

    @GetMapping("/list")
    @SaCheckPermission("ds:student:list")
    public Result<List<Student>> list(@ModelAttribute StudentDto studentDto) {
        return Result.success(studentService.listStudent(studentDto));
    }

    @GetMapping("/page")
    @SaCheckPermission("ds:student:page")
    public Result<PageResult<Student>> page(@ModelAttribute StudentDto studentDto) {
        return Result.success(studentService.pageStudent(studentDto));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @SaCheckPermission("ds:student:get")
    public Result<Student> selectOne(@PathVariable Long id) {
        return Result.success(studentService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param student 实体对象
     * @return 新增结果
     */
    @PostMapping
    @SaCheckPermission("ds:student:insert")
    public Result<Student> insert(@RequestBody Student student) {
        return Result.success(studentService.addStudent(student));
    }

    /**
     * 修改数据
     *
     * @param student 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateById")
    @SaCheckPermission("ds:student:update")
    public Result<Boolean> updateById(@RequestBody Student student) {
        boolean success = studentService.updateById(student);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }


    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("ds:student:delete")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = studentService.removeById(id);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.DELETE_FAIL);
    }

    @GetMapping("/pageByLesson")
    @SaCheckPermission("ds:student:page")
    public Result<PageResult<Student>> pageByLesson(@ModelAttribute StudentDto studentDto) {
        ValueChecker.checkNotNull(studentDto.getLessonId(), "课程ID不能为空");
        return Result.success(studentService.pageByLesson(studentDto));
    }

    /**
     * 学员到场
     */
    @PostMapping("/arrive")
    @SaCheckPermission("ds:student:update")
    public Result<Boolean> arrive(@RequestBody StudentDto studentDto) {
        ValueChecker.checkNotNull(studentDto.getLessonId(), "课程ID不能为空");
        ValueChecker.checkNotNull(studentDto.getStudentId(), "学员ID不能为空");
        return Result.success(studentService.arrive(studentDto.getLessonId(), studentDto.getStudentId()));
    }

    /**
     * 取消预约
     */
    @PostMapping("/cancelLesson")
    @SaCheckPermission("ds:student:update")
    public Result<Boolean> cancelLesson(@RequestBody StudentDto studentDto) {
        ValueChecker.checkNotNull(studentDto.getLessonId(), "课程ID不能为空");
        return Result.success(studentService.cancelLesson(studentDto.getLessonId()));
    }
    /**
     * 签到
     */
    @PostMapping("/signInLesson")
    @SaCheckPermission("ds:student:update")
    public Result<Boolean> signInLesson(@RequestBody StudentDto studentDto) {
        ValueChecker.checkNotNull(studentDto.getLessonId(), "课程ID不能为空");
        ValueChecker.checkNotNull(studentDto.getPassword(), "签到密码不能为空");
        return Result.success(studentService.signInLesson(studentDto.getLessonId(), studentDto.getPassword()));
    }
}

